<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MadiChat - 登录</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body {
            background-color: #f8f9fa;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .login-container {
            max-width: 400px;
            width: 100%;
            padding: 2rem;
            background-color: white;
            border-radius: 1rem;
            box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
        }
        .logo {
            text-align: center;
            margin-bottom: 2rem;
        }
        .logo img {
            width: 80px;
            height: 80px;
        }
        .form-control {
            border-radius: 0.5rem;
            padding: 0.75rem 1rem;
        }
        .btn-primary {
            background-color: #4e73df;
            border: none;
            border-radius: 0.5rem;
            padding: 0.75rem;
            font-weight: 600;
        }
        .btn-primary:hover {
            background-color: #2e59d9;
        }
        .toggle-form {
            text-align: center;
            margin-top: 1rem;
        }
        .toggle-form a {
            color: #4e73df;
            text-decoration: none;
        }
        .toggle-form a:hover {
            text-decoration: underline;
        }
        #registerForm {
            display: none;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="logo">
            <img src="https://images.unsplash.com/photo-1611162617213-7d7a39e9b1d7?ixlib=rb-1.2.1&auto=format&fit=crop&w=1000&q=80" alt="MadiChat Logo" class="rounded-circle">
            <h2 class="mt-3">MadiChat</h2>
            <p class="text-muted">与AI模型的智能对话</p>
        </div>

        <!-- 登录表单 -->
        <form id="loginForm">
            <div class="mb-3">
                <label for="loginPhone" class="form-label">手机号</label>
                <div class="input-group">
                    <span class="input-group-text"><i class="fas fa-mobile-alt"></i></span>
                    <input type="tel" class="form-control" id="loginPhone" placeholder="请输入手机号" required>
                </div>
            </div>
            <div class="mb-3">
                <label for="loginPassword" class="form-label">密码</label>
                <div class="input-group">
                    <span class="input-group-text"><i class="fas fa-lock"></i></span>
                    <input type="password" class="form-control" id="loginPassword" placeholder="请输入密码" required>
                </div>
            </div>
            <div class="mb-3 form-check">
                <input type="checkbox" class="form-check-input" id="rememberMe">
                <label class="form-check-label" for="rememberMe">记住我</label>
                <a href="#" class="float-end">忘记密码?</a>
            </div>
            <button type="button" class="btn btn-primary w-100" id="loginBtn">登录</button>
            <!-- <div class="toggle-form">
                <p>还没有账号? <a href="#" onclick="toggleForm()">立即注册</a></p>
            </div> -->
        </form>


    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <script src="./js/third/axios.min.js"></script>
    <script>
        // 页面加载时检查是否已登录
        document.addEventListener('DOMContentLoaded', function() {
            // 检查localStorage中是否存在token
            const token = localStorage.getItem('token');
            if (token) {
                // 已登录，直接跳转到聊天页面
                window.location.href = 'chat.html';
            }
        });

        document.getElementById('loginBtn').addEventListener('click', async function() {
            // 获取输入的手机号和密码
            const phone = document.getElementById('loginPhone').value.trim();
            const password = document.getElementById('loginPassword').value.trim();
            
            // 简单验证
            if (!phone) {
                alert('请输入手机号');
                return;
            }
            if (!password) {
                alert('请输入密码');
                return;
            }
            
            try {
                // 使用http.js中的login函数
                const response = await import('./js/http.js').then(module => {
                    return module.login(phone, password);
                });
                
                // 解析响应
                const data = response.data;
                
                if (data.success) {
                    // 登录成功，保存token到本地存储
                    localStorage.setItem('token', 'Bearer ' + data.result);
                    // 跳转到聊天页面
                    window.location.href = 'chat.html';
                } else {
                    // 登录失败，显示错误信息
                    alert(data.message || '登录失败，请检查手机号和密码');
                }
            } catch (error) {
                console.error('登录请求出错:', error);
                alert('登录请求失败，请检查网络连接');
            }
        });
    </script>
</body>
</html>